package com.s69.demo.controller;

import com.s69.demo.dto.LoginDto;
import com.s69.demo.service.LoginService;
import com.s69.demo.utils.SessionUtils;
import com.s69.demo.vo.LoginVo;
import com.s69.demo.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class LoginController {

    @Autowired
    LoginService loginService;

    @PostMapping("/login")
    public ResultVo login(@RequestBody LoginDto loginDto) {
        try {
            loginDto = loginService.login(loginDto);
            return ResultVo.success("", loginDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }

    @GetMapping("/logout")
    public ResultVo logout(HttpServletRequest request){
        String token = request.getHeader("token");
        SessionUtils.remove(token);
        return ResultVo.success("退出成功",null);
    }

}
